/**
 * Copyright  2025, Hai Yue Xing He Technology Co., Ltd
 * 
 * @author        ZHAO LIMIN
 * @version       1.0.1
 * @since         2025-04
 * StageController.ts   是传送门的控制器，利用Team 标志，进行
 *                      跳跃
 */
import { _decorator, Component, error, Node, Sprite } from 'cc';
import { HImageResource } from './HResource';
import { IParametersOptions } from '../hyxh/core/HBase';
import { SecenseTools } from './SecenseTools';
import { SecenesManager } from './SecenesManager';
import { defined } from '../hyxh/core/Defined';
import { SpritePlayer } from './SpritePlayer';
import { ICartesian } from '../hyxh/core/Cartesian';
import { Cartesian3 } from '../hyxh/core/Cartesian3';
import { BelialHunter } from './BelialHunter';
import { Cion } from './cion';
const { ccclass, property } = _decorator;

@ccclass('StageController')
export class StageController extends Component {
    fath_node: Node | null = null;

    /* 在路径网络的位置 */
    private _node_pos: Cion = null!;

    /* Stage Node  */
    private thisNode : Node = null;

    /**
     * cocos 的精灵或者图片对象
     */
    @property('sprite')
    private _sprite: Sprite = null!;
    private _image = 0; 
    private _path = -1;
    private _index = -1;
    private _team = -1;

    public moveForNodePos( new_node_pos: Cion) {
        if (!defined(new_node_pos)) {
            return;
        }
        this._node_pos = new_node_pos;
        this._sprite.node.setPosition(this._node_pos.position.x, this._node_pos.position.y, this._node_pos.position.z);
    }

    hasSamePosition(node_pos: Cion): boolean {
        if (!defined(this._node_pos)) {
            return false;
        }
        if ( this._node_pos === node_pos) {
            return true;
        }
        if ( this._node_pos.equalsPosition(node_pos) ) {
            return true; 
        }
        return false;
    }

    public getNodePos(): Cion {
        return this._node_pos;
    }

    public getTeam() {
        return this._team;
    }
    /** 图像资源 */
    private _spriteFrame: HImageResource = null!;

    /* 活跃状态, 激活后，启动跳跃 */
    public activity: boolean = false;


    /* 乘客, 用于进行电梯联动 */
    private passenger: SpritePlayer | BelialHunter | null = null;

    public setPassenger(passenger: SpritePlayer | BelialHunter) {
        this.passenger = passenger;
    }

    public getPassenger(): SpritePlayer | BelialHunter | null {
        return this.passenger;
    }

    public passengerLeave() {
        this.passenger = null;
    }

    public onEventForPassage: (spritePlay: SpritePlayer, hunterPosition: ICartesian) => boolean = null!;

    @property('seceneController')
    private _secenController: SecenseTools = null!;
    /**
     * 
     * @param option {
     *                        "name":"gatewayE101H",
     *                        "image":9001022,
     *                        "path": 9,
     *                        "index":-1,
     *                         "team": 1
     *                  },
     */
    public initialize(option: IParametersOptions) {
        if (!defined(option)) {
            return; 
        }
        this._image = option.image;
        this.name = option.name;
        this._path = option.path;
        this._index = option.index;    
        this._team = option.team;    
        /* 初始化node */
        this.thisNode = new Node(this.name);
        this.thisNode.parent = this.node;
    }

    // private showImage(imageResource: HImageResource ) {
    //     if ( !defined(this._sprite) ) {
    //         /* 增加显示 */
    //         this._sprite = this.node.addComponent(Sprite);
    //     }
    //     if (defined(imageResource)) {
    //         this._sprite.spriteFrame = imageResource.spriteFrame; 
    //     }
    //     if (defined(imageResource.frames)) {
    //         const x_scale = imageResource.frames.size[0]/imageResource.frames.sourceSize[0];
    //         const y_scale = imageResource.frames.size[1]/imageResource.frames.sourceSize[1];
    //         this.node.setScale( x_scale, y_scale, 0.0 );
    //     }
    // }

    /* 定位 */
    public location(location_x: number, location_y:number, location_z:number) {        
        this.thisNode.setPosition(location_x, location_y,  location_z);                     // 设置节点的位置        
    }

    public getLocation(): ICartesian {
        return new Cartesian3(this.node.getPosition().x,
                              this.node.getPosition().y,
                              this.node.getPosition().z);
    }

    public setSceneController(secenController: SecenseTools) {
        this._secenController = secenController; 
    }

    public loadResources() {        
        this.initSpriteImages(); 
    }

    public initSpriteImages() {
        if (!defined(this._secenController)) {
            error("option seceneController is null!");
        }
        const _path_index = this._path;
        const _p_index = this._index;
        if (!defined(_path_index)) {
            error("option path is null!");
        }

        if (!defined(_p_index)) {
            error("option point index is null!");
        }

        const _cion =  this._secenController.indexCion(_path_index, _p_index);
        if (!defined(_cion)) {
            error("path and point index is legal value , no cion has found!");
        }
        this._spriteFrame = SecenesManager.getInstance().getResource(this._image) as HImageResource;
        /* 显示传送门 */        
        SecenesManager.showImage(this.thisNode, this._spriteFrame);
        /* 定位 */
        this.location(_cion.position.x, _cion.position.y, 0.0);     
        this._node_pos = _cion;
    }
    /**
     * 表示有精灵进入
     */
    public transmit() {
        /* 用于跳跃 */

    }    
}


